package tt.com.bodybeautymanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import tt.com.bodybeautymanager.mapper.DeadCosmeticMapper;
import tt.com.bodybeautymanager.service.DeadCosmeticService;
import tt.com.common.enums.Status;
import tt.com.common.model.Dead;
import tt.com.common.model.DeadCosmetic;
import tt.com.common.util.CommonResult;
import tt.com.common.util.PageUtil;

import java.util.List;

@Service
public class DeadCosmeticImpl extends ServiceImpl<DeadCosmeticMapper,DeadCosmetic> implements DeadCosmeticService {
    @Override
    public CommonResult showDeadCosmetic(PageUtil pageUtil) {
        IPage<DeadCosmetic> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadCosmetic> queryWrapper = new QueryWrapper<>();
        IPage<DeadCosmetic> page1 = baseMapper.selectPage(page,queryWrapper);
        pageUtil.setTotalSize((int)page1.getTotal());
        pageUtil.setData(page1);
        if (pageUtil==null&& pageUtil.equals("")){
            return CommonResult.buildR(Status.DEFAULT,"数据库内容为空");
        }else
            return CommonResult.buildR(Status.OK,"显示成功",pageUtil);
  }

  //伪删除
    @Override
    public CommonResult deleteDeadCosmetic(DeadCosmetic deadCosmetic) {
        CommonResult result =null;
        DeadCosmetic deadCosmetic1 = baseMapper.selectById(deadCosmetic.getId());
        if (deadCosmetic1==null){
            return CommonResult.buildR(Status.DEFAULT,"没有该数据，无法删除");
        }else if (deadCosmetic.getDataStatus()==1){
            deadCosmetic.setDataStatus(0);
        }else {
            return CommonResult.buildR(Status.OK,"该数据以及删除");
        }
        if (baseMapper.updateById(deadCosmetic)==1){
            return CommonResult.buildR(Status.OK,"该条数据已删除");
        }else {
            return CommonResult.buildR(Status.DEFAULT,"删除失败");
        }
    }

    @Override
    public CommonResult updateCosmetic(DeadCosmetic deadCosmetic) {
        if (deadCosmetic==null){
            return CommonResult.buildR(Status.DEFAULT,"没有该数据，无法修改");
        }else
            baseMapper.updateById(deadCosmetic);
            return CommonResult.buildR(Status.OK,"修改成功");
            
    }

        //刷新页面
    @Override
    public CommonResult refreshDeadCosmetic(PageUtil pageUtil) {
        List<DeadCosmetic> list = baseMapper.selectList(null);
        pageUtil.setPageSize(list.size());
        IPage<DeadCosmetic> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadCosmetic> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("data_status",0);
        IPage<DeadCosmetic> deadCosmetic = baseMapper.selectPage(page,queryWrapper);
        pageUtil.setData(deadCosmetic);
        return CommonResult.buildR(Status.OK,"刷新成功",pageUtil);
    }


    @Override
    public CommonResult selectCosmetic(DeadCosmetic deadCosmetic, PageUtil pageUtil) {
        List<DeadCosmetic> list = baseMapper.selectList(null);
        IPage<DeadCosmetic> page = new Page<>(pageUtil.getPageIndex(),pageUtil.getPageSize());
        QueryWrapper<DeadCosmetic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",deadCosmetic.getId());
        IPage<DeadCosmetic> page1 = baseMapper.selectPage(page,queryWrapper);
        if (page1==null){
            return CommonResult.buildR(Status.DEFAULT,"查询结果为空");
        }else {
            return CommonResult.buildR(Status.OK,"查询成功，已返回查询结果",page1);
        }
    }
}